package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	file, err := os.Open("D:\\Code\\Go\\GoStudy\\src\\go_code\\project5\\file\\a.txt")
	if err != nil {
		fmt.Println(err)
	}
	println(file)
	file.Close()

	//缓存读取  默认4096
	reader := bufio.NewReader(file)
	for {
		str, err := reader.ReadString('\n') //读到换行就介绍
		println(str)
		if err == io.EOF { //判断是否为最后一行
			break
		}
		fmt.Println(str)
	}

	//新文件的编辑
	filePath := "D:\\Code\\Go\\GoStudy\\src\\go_code\\project5\\file\\a.txt"
	os.Stat(filePath) //判断文件是否存在
	openFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		println("打开文件错误")
	}
	defer file.Close()
	writer := bufio.NewWriter(openFile)
	for i := 0; i < 2; i++ {
		writer.WriteString("ssdfsdf") //???
	}

	io.Copy(writer, reader)

}
